#!/bin/bash
# Bash Menu Script Example

#set -x

SCRIPT_FULL_PATH=`readlink -f $0`
ROOT=`dirname $SCRIPT_FULL_PATH`

TOOL_HOME=$ROOT/codegen
RUNTIME_SRC=$TOOL_HOME/runtime/src
SERVICE_HOME=$ROOT/service
CLIENT_HOME=$ROOT/client

hessianc_jar=$TOOL_HOME/release/hessianc.jar
service_jar=$SERVICE_HOME/release/service.jar

function usage() {
   echo "usage: gen.sh {module} {module params}"
   echo -e  "\ttool\tclean release  (generate hessianc.jar)"
   
   echo -e  "\truntime\tclean release  (generate runtime libhessianorb.a)"

   echo -e  "\tservice\tclean release  (generate service.jar with interface)"

   echo -e  "\tclient (generate client stub)"

   echo -e  "\tclientTest (generate clientTest)"
}

function genTool() {
   enterDir $TOOL_HOME
   ant ${@:1}
}

function genRuntime() {
   enterDir $RUNTIME_SRC
   make ${@:1}   
}

function genService() {
   enterDir $SERVICE_HOME
   ant ${@:1}
}

function genClient() {
   rm -rf $CLIENT_HOME
   mkdir $CLIENT_HOME
   java -classpath ${hessianc_jar} com.googlecode.hessianorb.Launcher -classpath ${service_jar} -header-dest ${CLIENT_HOME} -source-dest ${CLIENT_HOME} test test.IService   
}

function genClientTest() {
   cp test.cpp $CLIENT_HOME
   cp makefile_clientTest $CLIENT_HOME/makefile
   enterDir $CLIENT_HOME
   make ${@:1}   
}

function enterDir() {
   echo "Enter directory $1"
   cd $1
}

if [ $# -eq 0 ]; then
   usage
   exit
fi

case "$1" in
tool)
   genTool ${@:2} 
   ;;

runtime)
   genRuntime ${@:2}
   ;;  

service)
   genService ${@:2}
   ;; 

client)
   genClient ${@:2}
   ;; 

clientTest)
   genClientTest ${@:2}
   ;;

*)
   usage
esac
